package com.hula.onion.book.server.spider.platforms.xiaoyinglicai;

import com.hula.onion.book.server.common.CommonResponse;
import com.hula.onion.book.server.common.LoginForm;
import com.hula.onion.book.server.config.ResponseStatusEnum;
import com.hula.onion.book.server.model.AccountInfo;
import com.hula.onion.book.server.utils.MD5Utils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * Description:
 * Create Date:2018/1/17 19:49
 *
 * @author shan
 * @version 1.0
 */
@Component
public class XiaoYingLiCaiSpiderHelper {

    /**
     * 组装请求参数
     *
     * @param form
     * @return
     */
    public Map<String, Object> installRequestParams(LoginForm form) {

        Map<String, Object> map = new HashMap<>(5);

        map.put("username", form.getUserName());
        map.put("password", MD5Utils.MD5Encode(form.getPassword()));
        map.put("userType", 0);
        map.put("_fromAjax_", 1);
        map.put("_csrfToken_", "d41d8cd98f00b204e9800998ecf8427e");
        return map;

    }

    /**
     * 解析账户余额
     *
     * @param html
     * @return
     */
    public CommonResponse parseAccountBalance(String html) {

        try {
            AccountInfo accountInfo = new AccountInfo();

            Document dc = Jsoup.parse(html);

            //总资产
            String amount = dc.selectFirst("li.sum-item.frist").childNode(3).childNode(1).childNode(0).outerHtml();
            String totalIntrest = dc.getElementById("incomeDetail").child(0).text();
            accountInfo.setAmount(new BigDecimal(amount));
            accountInfo.setTotalIntrest(new BigDecimal(totalIntrest));
            return new CommonResponse(accountInfo);
        } catch (Exception ex) {
            return new CommonResponse(ResponseStatusEnum.QUERY_FAIL);
        }

    }

}
